<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<script src="js/jquery-1.4.2.min.js"></script>
<script src="js/common.js?v=1.0"></script>
<script src="js/consumer.js?v=1.3"></script>
<script src="js/oauth.js?v=1020"></script>
<script src="js/sha1.js"></script>
<script language="JavaScript"> 
var thePort = {};
var state; 
var t;
var OAuth = OAuth || {};
var defaultSettingData = {"remind_time":5, "remind_pop":1, "remind_comment":1, "remind_fans":1, "remind_msg":1,"remind_atme":1,"remind_stop":0};
var accessToken = localStorage.getItem("oauth2_accessToken");
var accessor = consumer["sina"];

//获取新信息
function getUnreadInfo(showBalloon) {
	var message = {
		method: "get", action:accessor.serviceProvider.unreadURL
		, parameters: [["scope", "http://www.google.com/m8/feeds/"]]
	};
	var requestBody = OAuth.formEncode(message.parameters);
	var request = newXMLHttpRequest();
	request.onreadystatechange = function receiveData() {
		if (request.readyState == 4) {
           if (request.status == 200){
				console.log(request.responseText);
				var results = JSON.parse(request.responseText);
				var num = parseInt(results["status"]) + parseInt(results["followers"]) + parseInt(results["dm"]) + parseInt(results["mentions"]) + parseInt(results["comments"]);
				var settingStr = localStorage.getItem("_WEIBO_REMIND_SETTING");
				if(checkIfLocal(settingStr)){
					var settingData = JSON.parse(settingStr);
				} else {
					var settingData = defaultSettingData;
				}
				if(num > 0 && settingData["remind_stop"] == 0){
					chrome.browserAction.setBadgeBackgroundColor({color:[255,30,0,100]});
					chrome.browserAction.setBadgeText({text:num});
					thePort.postMessage({cmd:"unread", data:results});
				} else {
					chrome.browserAction.setBadgeText({text:""});
				}

				var num1 = num - parseInt(results["status"]);
				

				if(settingData["remind_pop"] == 1 && settingData["remind_stop"] == 0 && num1 > 0 && showBalloon == 1){
					var str = "";
					if(parseInt(results["followers"]) > 0){
						str += "有" + results["followers"] + "个新粉丝\n";
					}	
					if(parseInt(results["dm"]) > 0){
						str += "有" + results["dm"] + "条新私信\n";
					}	
					if(parseInt(results["mentions"]) > 0){
						str += "有" + results["mentions"] + "条微博提到我\n";
					}
					if(parseInt(results["comments"]) > 0){
						str += "有" + results["comments"] + "条新评论";
					}
					if(str != ""){
						chrome.browserAction.showBalloon({ 
							title: "微博提示", 
							content:str
						});
					}
				}		
							
			}
        }
    };

	request.open(message.method, message.action, true); 
	request.setRequestHeader("Authorization", "OAuth2 " + accessToken);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(requestBody);
}

function listen(){
	chrome.extension.onConnect.addListener(function (port) {
		thePort = port;
		port.onMessage.addListener(function(request) {
			switch (request.cmd) {
				case "openpop":
					getUnreadInfo(0);
				break;

				case "resetunread":
					getUnreadInfo(2);
				break;

				case "read":
					setTimeout(function(){getUnreadInfo(0)},5000);
				break;

				case "newaccess":
					accessToken = localStorage.getItem("oauth2_accessToken");
					console.log(accessToken);
					getUnreadInfo();
					init();
				break;

				case "logout":
					clearInterval(t);
				break;
			}
		});
	});
}

function init(){
	var settingStr = localStorage.getItem("_WEIBO_REMIND_SETTING");
	if(checkIfLocal(settingStr)){
		var settingData = JSON.parse(settingStr);
	} else {
		var settingData = defaultSettingData;
	}
	var remindTime = parseInt(settingData["remind_time"]);
	remindTime = remindTime*1000*60;
	t = setInterval("getUnreadInfo(1);", remindTime);
}

function renewUser(data){
	localStorage.setItem("oauth2_userinfo", data);
}

var menuItems = [
	{
		type: "normal",
		title: "打开新浪微博",
		contexts: ["mainframe"],
		onclick: function(info, tab){chrome.browserAction.showPopup();}
	},
	{
		type: "normal",
		title: "发长微博",
		contexts: ["mainframe"],
		onclick: function(info, tab){
			chrome.windows.create({url:"long.html",width:600,height:610,left:400,top:200,type:"popup"});
		}
	},
	{
		type: "normal",
		title: "发图片",
		contexts: ["mainframe"],
		onclick: function(info, tab){
			chrome.windows.create({url:"image.html",width:600,height:410,left:400,top:200,type:"popup"});
		}
	},
	{
		type: "normal",
		title: "访问新浪微博",
		contexts: ["mainframe"],
		onclick: function(info, tab){
                    chrome.tabs.create({
                                url: "http://www.weibo.com/",
                                selected: true,
                                index: tab.index + 1
                            });
                }
	}

];
for (var i in menuItems) {
	//chrome.contextMenus.create(menuItems[i]);
}



$(document).ready(function(){
	var userInfoData = {};
	var userInfoStr = localStorage.getItem("oauth2_userinfo");
	if(userInfoStr == undefined){
	}
	if(checkIfLocal(userInfoStr)){
		userInfoData = JSON.parse(userInfoStr);
		if(userInfoData["id"]){
			var uid = userInfoData["id"];
			getUser(uid, OAuth, accessor, accessToken, renewUser);
			init();
			getUnreadInfo(1);
		}
	}
	listen();
});

</script> 

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33560959-3']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</head> 
<body> 
</body> 
</html> 
